package com.cdvtc.news.web.admin;

import com.cdvtc.news.entity.Admin;
import com.cdvtc.news.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller("adminLoginController")
@RequestMapping("/admin")
public class LoginController {
    @Autowired
    private AdminService adminService;

    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String index(){
        return "admin/login";
    }

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String login(String account, String password, Boolean remember, Model model, HttpServletResponse response, HttpSession session){
        Admin admin = adminService.login(account, password);
        if (admin != null) { //登陆成功
            session.setAttribute("admin", admin);

            // 处理记住密码
            if (remember) {
                Cookie cookie = new Cookie("adminLogin", account+"::"+password);
                cookie.setMaxAge(60 * 60 * 24 * 30); //失效时间为30天
                response.addCookie(cookie); //向用户浏览器发送Cookie，下次请求时生效
            }

            return "redirect:/admin";
        } else { //登陆失败
            model.addAttribute("error", "账户或密码错误！");
            return "admin/login";
        }
    }

    @RequestMapping("autoLogin")
    public String autoLogin(HttpServletRequest request) {
        String adminLogin =  null;
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("adminLogin")){
                adminLogin = cookie.getValue();
            }
        }

        if(adminLogin != null) {
            String[] loginStr = adminLogin.split("::");

            Admin admin = adminService.login(loginStr[0], loginStr[1]);
            if (admin != null) { //登陆成功
                request.getSession().setAttribute("admin", admin);

                String rootPath = request.getContextPath();
                return "redirect:/admin";
            } else { //登陆失败，进入登陆页面，不提示错误
                return "admin/login";
            }
        } else {
            return "redirect:/admin/login";
        }
    }

    @RequestMapping("logout")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        // 删除 Cookie数据
        Cookie[] cookies = request.getCookies();
        for(Cookie cookie: cookies) {
            if(cookie.getName().equals("adminLogin")){
                cookie.setMaxAge(0);
                response.addCookie(cookie);
                break;
            }
        }

        request.getSession().invalidate();

        return "redirect:/admin/login";
    }
}
